package com.googlecode.connectlet.crypto;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.Provider;

import com.googlecode.connectlet.crypto.digest.MD5MessageDigest;
import com.googlecode.connectlet.crypto.keystore.CompactKeyStore;
import com.googlecode.connectlet.crypto.random.RC4PrngCipher;
import com.googlecode.connectlet.crypto.random.RC4PrngSecureRandom;
import com.googlecode.connectlet.crypto.rsa.provider.MD5withRSA;
import com.googlecode.connectlet.crypto.rsa.provider.RSAKeyFactory;
import com.googlecode.connectlet.crypto.rsa.provider.RSAKeyPairGenerator;

public final class EasyProvider extends Provider {
	private static final long serialVersionUID = 1L;

	private static volatile EasyProvider instance = null;

	public static EasyProvider getInstance() {
		if (instance == null) {
			synchronized (RC4PrngCipher.class) {
				if (instance == null) {
					instance = new EasyProvider();
				}
			}
		}
		return instance;
	}

	private EasyProvider() {
		super("EasyProvider", 1.0, "EasyProvider");
		AccessController.doPrivileged(new PrivilegedAction<Object>() {
			@Override
			public Object run() {
				put("SecureRandom.RC4PRNG", RC4PrngSecureRandom.class.getName());
				put("MessageDigest.MD5", MD5MessageDigest.class.getName());
				put("Signature.MD5withRSA", MD5withRSA.class.getName());
				put("KeyPairGenerator.RSA", RSAKeyPairGenerator.class.getName());
				put("KeyFactory.RSA", RSAKeyFactory.class.getName());
				put("KeyStore.Compact", CompactKeyStore.class.getName());
				put("AlgorithmParameters.IV", IvParameters.class.getName());
				return null;
			}
		});
	}
}